/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Maurice
 */
public class XmlReader 
{
    DocumentBuilderFactory Factory;
    DocumentBuilder Builder;
    Document Doc;
    
    public XmlReader () throws ParserConfigurationException
    {
            Factory=DocumentBuilderFactory.newInstance();
            Builder= Factory.newDocumentBuilder();
    }
    public void readServerMessages() throws SAXException, IOException
    {
        Doc=Builder.parse(new File("./ServerMessages.xml"));
        String welcomeMessage;
        List<String> serverMessages=new ArrayList<String>();
        
        if(Doc.hasChildNodes())  
        {
             NodeList nList=Doc.getElementsByTagName("welcomeMessage");
             
             if(nList.getLength()==1)
             {
                 welcomeMessage=nList.item(0).getTextContent();
             }
             nList=Doc.getElementsByTagName("otherMessages");
             
             
             for(int i=0;i<nList.getLength();i++)
             {
                 Node otherMsg=nList.item(i);
                 Element otherMessages=(Element) otherMsg;
                 
                 NodeList nLMessages= otherMessages.getElementsByTagName("message");
                 
                 for(int messageNumber=0;messageNumber<nLMessages.getLength();messageNumber++)
                 {
                     serverMessages.add(nLMessages.item(messageNumber).getTextContent());
                 } 
             }
             
        }
    }
    public List<String> readUserMessages(String userName) throws SAXException, IOException
    {
        Doc=Builder.parse(new File("./UserMessages.xml"));
        String welcomeMessage;
        List<String> UserMessages=new ArrayList<String>();
        
        if(Doc.hasChildNodes())  
        {
             NodeList nList=Doc.getElementsByTagName("player");
             
             if(nList.getLength()>=1)
             {
                 for(int i=0;i<nList.getLength();i++)
                {
                    if(nList.item(i).getTextContent().equals(userName))
                    {
                        Node nPlayer=nList.item(i);
                        Element player=(Element) nPlayer;
                        
                        NodeList nMessages=player.getElementsByTagName("Message");
                        
                        for (int messageNumber=0;messageNumber<nMessages.getLength();messageNumber++)
                        {
                            UserMessages.add(nMessages.item(messageNumber).getTextContent());
                        }
                    }
                    
                }
             }
             else
             {
                 UserMessages.add("No Messages");
             }
             
             
        }
        
        return UserMessages;
    }
}
